<?php
namespace app\index\controller;

use app\common\model\SeedingSecond as SeedingSecondModel;
use app\common\model\FruitSecondLog as FruitSecondLogModel;

use app\common\component\seed\SeedService;
use app\common\component\fruit\FruitService;

class FruitSecond extends ControllerBase {
	public function _initialize() {
		$this -> checkLogin();
	}

	public function index() {
    	$userId = \think\Session::get('user_id');

		// 获取每天收益
		$seedingSecondServiceGetProfit = (new \app\common\component\seeding\SeedingSecondService()) -> getProfit($userId);
		$this -> view -> assign('seedGrowUpTotal', $seedingSecondServiceGetProfit['data']['total']);

		$currentTime = time();

		$fruitServiceFind = (new FruitService()) -> find($userId);
		if(isset($fruitServiceFind['data']['fruit']['fruit_second_profit'])) {
			$this -> view -> assign('fruitSecondProfit', $fruitServiceFind['data']['fruit']['fruit_second_profit']);
		} else {
			$this -> view -> assign('fruitSecondProfit', 0);
		}

		$seedServiceFind = (new SeedService()) -> find($userId);
		if(isset($seedServiceFind['data']['seed']['seed_second'])) {
			$this -> view -> assign('seedSecond', $seedServiceFind['data']['seed']['seed_second']);
		} else {
			$this -> view -> assign('seedSecond', 0);
		}

		$selectSeeding = SeedingSecondModel::where('user_id = ' . $userId . ' AND status in(3, 4)') -> select();
		$seedings = [];
		foreach($selectSeeding as $seeding) {
			$seedings[$seeding['sort']] = $seeding;
		}

		$this -> view -> assign('seedings', $seedings);

        return $this -> view -> fetch();
    }

    public function record() {
		$this -> view -> assign('fruitSecondLogs', FruitSecondLogModel::where('user_id', \think\Session::get('user_id')) -> order('fruit_second_log_id DESC') -> paginate(10, false));

        return $this -> view -> fetch();
    }
}
?>